//
//  ProfileVC.swift
//  Drop
//
//  Created by zhuxuhong on 2018/3/16.
//  Copyright © 2018年 zhuxuhong. All rights reserved.
//

import UIKit
import Dialog

class ProfileVC: UITableViewController {

	@IBOutlet weak var headerIV: UIImageView!
	@IBOutlet weak var avatarBtn: UIButton!
    @IBOutlet weak var informationLabel: UILabel!
	
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.tableFooterView = UIView()
        
        setUserInformation()
    }
    
    fileprivate func setUserInformation(){
        let name = NSAttributedString(string: User.current.name, font: Font.p18.boldSystem, color: .white, alignment: .center)
        let motto = NSAttributedString(string: "\n\(User.current.motto)", font: Font.p14.system, color: UIColor.white.alpha(0.7), alignment: .center)
        informationLabel.attributedText = NSMutableAttributedString(attrStrings: [name, motto])
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        tableView.contentOffset = CGPoint(x: 0, y: app.statusBarFrame.height)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        setNavigationBar(isHidden: true, animated: false)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        setNavigationBar(isHidden: false)
    }
	
	@IBAction func changeAvatar(){
		func handleImagePicking(sourceType: UIImagePickerControllerSourceType){
            ImagePicker.shared.showWith(presentingViewController: self, sourceType: sourceType, allowsEditing: true, completion: { image in
				if let image = image{
					self.avatarBtn.setImage(image.withRenderingMode(.alwaysOriginal), for: .normal)
					self.headerIV.image = image
				}
			})
		}
		
		Dialog.default(actions: [
            .init(title: "相册", handler: { _ in
				handleImagePicking(sourceType: .savedPhotosAlbum)
			}),
			.init(title: "相机", handler: { _ in
				handleImagePicking(sourceType: .camera)
			}),
            .init(title: "取消", style: .cancel)
		])
	}
}
